package com.drool.mediaplayer.Adapters;

import com.drool.mediaplayer.R;

import android.content.Context;
import android.database.Cursor;

import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Video;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SongBrowserAdapter extends CursorAdapter{

	private static String TAG ="SongBrowserAdapter";
	private Cursor songAlbumCursor = null;
	private String currentSong;
	
	public SongBrowserAdapter(Context context, Cursor c, Cursor albumCursor, String currentSong) {
		super(context, c);
		songAlbumCursor = albumCursor;
		this.currentSong = currentSong;
		// TODO Auto-generated constructor stub
	}
	
	public void setCurrentSong(String currentSong){
		this.currentSong = currentSong;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		//Log.i("adapter", "Binding ID" + cursor.getString(0) );
		Long duration;
		
		TextView nameTxt = (TextView)view.findViewById(R.id.text1);
		TextView artistTxt = (TextView)view.findViewById(R.id.text3);
		TextView durationTxt = (TextView)view.findViewById(R.id.text2); 
		TextView descTxt = (TextView)view.findViewById(R.id.text4);
		ImageView albumArt = (ImageView) view.findViewById(R.id.albumImage);
		ImageView selectedImage = (ImageView) view.findViewById(R.id.selectedImage);
		
		nameTxt.setText(cursor.getString(cursor.getColumnIndex(Audio.Media.TITLE)));
		artistTxt.setText("Artist: " + cursor.getString(cursor.getColumnIndex(Audio.Media.ARTIST)));
		
		try{
			duration = Long.valueOf(cursor.getString(cursor.getColumnIndex(Audio.Media.DURATION)));
		}catch(Exception e){
			Log.e(TAG, "Duration error:",e);
			duration = 0L;
		}
		
		
		int minDuration = (int) ((duration / 1000) / 60);
		
		int secDuration = (int) ((duration / 1000) % 60);
		
		durationTxt.setText(Integer.toString(minDuration) + ":" + Integer.toString(secDuration));
		
		albumArt.setImageResource(R.drawable.musicdelta);
		if (cursor.getString(0).equals(currentSong)){
			selectedImage.setImageResource(R.drawable.musicdelta);
		}else
			selectedImage.setImageDrawable(null);
		
		if (songAlbumCursor.getCount()> 0){
			//Log.i(TAG, cursor.getString(cursor.getColumnIndex(Audio.Media.ALBUM_ID)));
			songAlbumCursor.moveToFirst();
			while(!songAlbumCursor.isAfterLast()){
				if (songAlbumCursor.getString(0).equals(cursor.getString(cursor.getColumnIndex(Audio.Media.ALBUM_ID)))){
					if (songAlbumCursor.getString(songAlbumCursor.getColumnIndex(Audio.Albums.ALBUM_ART)) != null)
						albumArt.setImageURI(Uri.parse(songAlbumCursor.getString(songAlbumCursor.getColumnIndex(Audio.Albums.ALBUM_ART))));
					break;
				}
				songAlbumCursor.moveToNext();
			}
		}
		
		if (cursor.getString(cursor.getColumnIndex(Audio.Media.ALBUM))!= null)
			descTxt.setText(cursor.getString(cursor.getColumnIndex(Audio.Media.ALBUM)));
		else
			descTxt.setText("Description: Unknown");
		
		//Log.i(TAG,cursor.getString(cursor.getColumnIndex(Video.Media.DURATION)) + "/" + Long.toString(duration));
		
		
		
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.layadapter, parent, false);
		bindView(v, context, cursor);
		return v;
	}
	
}
